﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
   public class ContactDAO//:BaseDAO<RELAXEntities,RelaxContact>
   {
       #region value
       private readonly RelaxDataContext _context = new RelaxDataContext();
       #endregion

       #region Hàm lấy dữ liệu

       public IQueryable<RelaxContactDataContract> GeContactDataContracts()
       {
           var lReturn = from c in _context.CONTACTs
               select new RelaxContactDataContract
               {
                   Id = Convert.ToInt32(c.id),
                   Name = c.name,
                   Company = c.company,
                   Address = c.address,
                   Tel = c.tel,
                   Email = c.mail,
                   Title = c.title,
                   Detail = c.detail,
                   Decription = c.decription,
                   Date = Convert.ToDateTime(c.date),
                   IsAnswer = Convert.ToInt32(c.isanswer),
                   Type = Convert.ToInt32(c.type),
                   Active = Convert.ToInt32(c.active)
               };
           return lReturn;
       }
       #endregion

       #region Add
       public void Add(CONTACT contact)
       {
           _context.CONTACTs.InsertOnSubmit(contact);
           _context.SubmitChanges();

       }
       #endregion

       #region Update
       public void Update(CONTACT contact)
       {
           var first = _context.CONTACTs.FirstOrDefault(c => c.id == contact.id);
           if (first != null)
           {
               first.name = contact.name;
               first.company = contact.company;
               first.address = contact.address;
               first.tel = contact.tel;
               first.mail = contact.mail;
               first.title = contact.title;
               first.decription = contact.decription;
               first.detail = contact.detail;
               first.date = contact.date;
               first.isanswer = contact.isanswer;
               first.type = contact.type;
               first.active = contact.active;
               _context.SubmitChanges();
           }
       }
       #endregion

       #region Delete

       public void Delete(long iId)
       {
           var first = _context.CONTACTs.FirstOrDefault(c => c.id == iId);
           if (first != null)
           {
               _context.CONTACTs.DeleteOnSubmit(first);
               _context.SubmitChanges();
           }
       }
       #endregion
   }
}
